home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / WASTE 1.2 / WASTE Demo ƒ / LongControls.c < prev    next >
Text File  |  1996-05-19  |  5KB  |  241 lines

  1. /*
  2.     WASTE Demo Project:
  3.     Macintosh Controls with Long Values
  4.  
  5.     Copyright © 1993-1996 Marco Piovanelli
  6.     All Rights Reserved
  7.  
  8.     C port by John C. Daub
  9. */
  10.  
  11. #ifndef __CONTROLS__
  12. #include <Controls.h>
  13. #endif
  14.  
  15. #ifndef __FIXMATH__
  16. #include <FixMath.h>
  17. #endif
  18.  
  19. #ifndef __TOOLUTILS__
  20. #include <ToolUtils.h>
  21. #endif
  22.  
  23. #ifndef __WEDEMOAPP__
  24. #include "WEDemoIntf.h"
  25. #endif
  26.  
  27.  
  28. // long control auxiliary record used for keeping long settings
  29. // a handle to this record is stored in the reference field of the control record
  30.  
  31. struct LCAuxRec
  32. {
  33.     long    value;    // long value
  34.     long    min;    // long min
  35.     long    max;    // long max
  36. };
  37. typedef struct LCAuxRec LCAuxRec, *LCAuxPtr, **LCAuxHandle;
  38.  
  39.  
  40. OSErr LCAttach( ControlRef control )
  41. {
  42.     Handle        aux;
  43.     LCAuxPtr    pAux;
  44.  
  45.     /*    allocate the auxiliary record that will hold long settings */
  46.  
  47.     if ( ( aux = NewHandleClear( sizeof( LCAuxRec ) ) ) == nil )
  48.     {
  49.         return    MemError( );
  50.     }
  51.  
  52.     /*    store a handle to the auxiliary record in the contrlRfCon field */
  53.  
  54.     SetControlReference( control, (long) aux );
  55.  
  56.     /*    copy current control settings into the auxiliary record */
  57.  
  58.     pAux = * (LCAuxHandle) aux;
  59.     pAux->value = GetControlValue( control );
  60.     pAux->min = GetControlMinimum( control );
  61.     pAux->max = GetControlMaximum( control );
  62.  
  63.     return noErr;
  64. }
  65.  
  66. void LCDetach( ControlRef control )
  67. {
  68.     Handle aux;
  69.  
  70.     if ( ( aux = (Handle) GetControlReference( control ) ) != nil )
  71.     {
  72.         SetControlReference( control, 0L );
  73.         DisposeHandle( aux );
  74.     }
  75. }
  76.  
  77. void LCSetValue( ControlRef control, long value )
  78. {
  79.     LCAuxPtr pAux;
  80.     short controlMin, controlMax, newControlValue;
  81.  
  82.     pAux = * (LCAuxHandle) GetControlReference( control );
  83.  
  84.     /*    make sure value is in the range min...max */
  85.  
  86.     if ( value < pAux->min )
  87.     {
  88.         value = pAux->min;
  89.     }
  90.     if ( value > pAux->max )
  91.     {
  92.         value = pAux->max;
  93.     }
  94.  
  95.     /*    save value in auxiliary record */
  96.  
  97.     pAux->value = value;
  98.  
  99.     /*    calculate new thumb position */
  100.  
  101.     controlMin = GetControlMinimum( control );
  102.     controlMax = GetControlMaximum( control );
  103.     newControlValue = controlMin + FixRound( FixMul ( FixDiv( value - pAux->min,
  104.                 pAux->max - pAux->min), BSL(controlMax - controlMin, 16 )));
  105.  
  106.     /*    do nothing if the thumb position hasn't changed */
  107.  
  108.     if ( newControlValue != GetControlValue(control) )
  109.     {
  110.         SetControlValue( control, newControlValue );
  111.     }
  112. }
  113.  
  114. void LCSetMin( ControlRef control, long min )
  115. {
  116.     LCAuxPtr pAux;
  117.  
  118.     pAux = * (LCAuxHandle) GetControlReference( control );
  119.  
  120.     /*    make sure min is less than or equal to max */
  121.  
  122.     if ( min > pAux->max )
  123.     {
  124.         min = pAux->max;
  125.     }
  126.  
  127.     /*    save min in auxiliary record */
  128.  
  129.     pAux->min = min;
  130.  
  131.     /*    set control minimum to min or SHRT_MIN, whichever is greater */
  132.  
  133.     SetControlMinimum( control, ( min >= SHRT_MIN ) ? min : SHRT_MIN );
  134.  
  135.     /*    reset value */
  136.  
  137.     LCSetValue( control, pAux->value );
  138. }
  139.  
  140. void LCSetMax( ControlRef control, long max )
  141. {
  142.     LCAuxPtr pAux;
  143.  
  144.     pAux = * (LCAuxHandle) GetControlReference( control );
  145.  
  146.     /*    make sure max is greater than or equal to min */
  147.  
  148.     if ( max < pAux->min )
  149.     {
  150.         max = pAux->min;
  151.     }
  152.  
  153.     /*    save max in auxiliary record */
  154.  
  155.     pAux->max = max;
  156.  
  157.     /*    set control maximum to max or SHRT_MAX, whichever is less */
  158.  
  159.     SetControlMaximum( control, ( max <= SHRT_MAX ) ? max : SHRT_MAX );
  160.  
  161.     /*    reset value */
  162.  
  163.     LCSetValue( control, pAux->value );
  164. }
  165.  
  166. /*    In each of these LCGetXXX() functions, there are 2 ways listed to do things.  They are
  167.     both the same thing and perform the same stuff, just one is easier to read than the
  168.     other (IMHO).  I asked Marco about it and he gave me the shorter code (what's commented
  169.     in each function) and gave me this explanation:
  170.  
  171.         This version [the commented code] yields smaller and faster code
  172.         (try disassembling both versions if you wish), but some people may
  173.         find it somewhat harder to read.
  174.  
  175.     I agree with Marco that his code is better overall, but in the interest of readabilty
  176.     (since this demo is a learning tool), I left my code in and put Marco's in commented
  177.     out.  Pick whichever you'd like to use.
  178. */
  179.  
  180. long LCGetValue( ControlRef control )
  181. {
  182.     LCAuxPtr    pAux;
  183.  
  184.     pAux = *((LCAuxHandle)GetControlReference( control ));
  185.  
  186.     return pAux->value;
  187.  
  188. //    this is Marco's code.  Remember, this is a little harder to read, but overall
  189. //    yields tighter code.
  190.  
  191. //    return (* (LCAuxHandle) GetControlReference(control)) -> value;
  192.  
  193. }
  194.  
  195. long LCGetMin( ControlRef control )
  196. {
  197.     LCAuxPtr    pAux;
  198.  
  199.     pAux = *((LCAuxHandle)GetControlReference( control ));
  200.  
  201.     return pAux->min;
  202.  
  203. //    this is Marco's code.  Remember, this is a little harder to read, but overall
  204. //    yields tighter code.
  205.  
  206. //    return (* (LCAuxHandle)GetControlReference(control)) -> min;
  207.  
  208. }
  209.  
  210. long LCGetMax( ControlRef control )
  211. {
  212.     LCAuxPtr    pAux;
  213.  
  214.     pAux = *((LCAuxHandle)GetControlReference( control ));
  215.  
  216.     return pAux->max;
  217.  
  218. //    this is Marco's code.  Remember, this is a little harder to read, but overall
  219. //    yields tighter code.
  220.  
  221. //    return (* (LCAuxHandle)GetControlReference(control)) -> max;
  222.  
  223. }
  224.  
  225. void LCSynch( ControlRef control )
  226. {
  227.     LCAuxPtr pAux;
  228.     short controlMin, controlMax, controlValue;
  229.  
  230.     controlMin = GetControlMinimum( control );
  231.     controlMax = GetControlMaximum( control );
  232.     controlValue = GetControlValue( control );
  233.     pAux = * (LCAuxHandle) GetControlReference( control );
  234.  
  235.     /*    calculate new long value */
  236.  
  237.     pAux->value = pAux->min + FixMul( FixRatio ( controlValue - controlMin,
  238.                   controlMax - controlMin), pAux->max - pAux->min );
  239. }
  240.  
  241.